package wo.bsys.advice;

import java.util.Arrays;
import java.util.Date;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import wo.bsys.po.OperationLog;
import wo.bsys.service.LogService;

@Aspect
@Component
public class OperationLogAspect {
	
	public OperationLogAspect() {
		super();
		LOG.info("new OperationLogAdvice2()");
	}

	private static Logger LOG = LogManager.getLogger(OperationLogAspect.class);
	
	@Autowired
	private LogService logService;
	
	// @Autowired
	// private OperationLogRepository operationLogRepository;
	
	// @Around ("within(wo.bsys.controller.*)")
	@Around ("bean(*ServiceImpl) && !bean(logServiceImpl)")
	public Object invoke(ProceedingJoinPoint pjp) throws Throwable {
		LOG.info(pjp.getSignature().getDeclaringTypeName());
		LOG.info(pjp.getSignature().getName());
		LOG.info(logService.getClass());
		OperationLog log = new OperationLog ();
		log.setClazz(pjp.getClass().getName());
		log.setMethod(pjp.getSignature().getName());
		log.setCreateTime(new Date());
		log.setCreateTimeLong(log.getCreateTime().getTime());
		log.setArgs(Arrays.toString(pjp.getArgs()));
		logService.save(log);
		Object re = pjp.proceed();
		return re;
	}
}
